package org.setamv.springcloud.controller;

import org.apache.commons.lang.time.DateUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.setamv.springcloud.dto.OpsResponse;
import org.setamv.springcloud.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.print.attribute.standard.Media;
import javax.validation.constraints.NotNull;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    // 本地的User数据库

    Map<String, User> usersMap = new HashMap<>();

    /**
     *
     * @param a {@link NotNull}
     */
    public UserController() {
        usersMap.put("1", new User("1", "setamv", "1985-11-13"));
        usersMap.put("2", new User("2", "susie", "1988-08-16"));
        usersMap.put("3", new User("3", "angel", "2013-08-29"));
        usersMap.put("4", new User("4", "hong", "2015-04-14"));
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public OpsResponse create(@RequestBody User user) {
        LOGGER.debug("create user: " + user);
        return OpsResponse.SUCCEED;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public User get(@PathVariable String id) throws Exception {
        LOGGER.debug("get user:{}", id);
        User user = new User(id, "setamv", "1985-11-13");
        return user;
    }

    @RequestMapping(value = "/{id}/slowly", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public User getSlowly(@PathVariable String id) throws Exception {
        LOGGER.debug("get user slowly:{}", id);
        User user = new User(id, "setamv", "1985-11-13");
        Thread.sleep(10000);
        return user;
    }

    /**
     * 批量根据ID获取用户信息
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/batch/{ids}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<User> getBatch(@PathVariable List<String> ids) throws Exception {
        LOGGER.debug("get batch user:{}", ids);
        List<User> users = new ArrayList<>();
        for (String id : ids) {
            User user = usersMap.get(id);
            if (user != null) {
                users.add(user);
            }
        }
        return users;
    }

    /**
     * 返回所有用户列表
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<User> list() throws Exception {
        LOGGER.debug("list user");
        List<User> users = new ArrayList<>();
        users.addAll(usersMap.values());
        return users;
    }

    /**
     * 以json的形式提交和返回数据
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/echo/by/json", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public User echoByJson(@RequestBody User user) throws Exception {
        LOGGER.debug("echo user by json");
        return user;
    }

    /**
     * 以xml的形式提交和返回数据
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/echo/by/xml", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
    public User getByXml(@RequestBody User user) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("<xml>")
          .append("  <user>")
          .append("     <id>").append(user.getId()).append("</id>")
          .append("     <name>").append(user.getName()).append("</name>")
          .append("     <birthDate>").append(user.getBirthDate()).append("</birthDate>")
          .append("  </user>")
          .append("</xml>");
        return user;
    }

}
